import { Number } from 'core-js'
import Vue, { del } from 'vue'
import Vuex from 'vuex'

import VuexPersistence from 'vuex-persist'
const vuexLocal = new VuexPersistence({
  storage: window.localStorage
})

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userName: '',
    token: '',
    cart: [],
    isRouter: ''
  },
  getters: {
    setcarsize (state) {
      let y = 0
      state.cart.forEach(i => {
        if (i.isEffective) {
          y += Number(i.count)
        }

      })
      return y
    },

    setcarsizes (state) {
      let y = 0
      state.cart.forEach(i => {
        if (i.isEffective && i.selected) {
          y += Number(i.count)
        }
      })
      return y
    },
    setcarvalues (state) {
      let y = 0
      state.cart.forEach(i => {
        if (i.isEffective && i.selected) {
          y += Number(i.count * i.price)
        }
      })
      return y
    }
  },
  mutations: {
    userName (state, val) {
      state.userName = val
    },
    del (state) {
      state.userName = []
    },
    settoken (state, token) {
      state.token = token
    },
    setcar (state, cart) {
      state.cart = cart
    },
    getisRouter (state, isRouter) {
      state.isRouter = isRouter
    },
  },
  actions: {
  },
  modules: {
  },
  plugins: [vuexLocal.plugin]
})
